<?php

/**
 * Description of UnderscoreGetters
 *
 * @author a.novikov
 */
abstract class UnderscoreGetters {

	/**
	 * Try to call function from parent object when ContentListElement have not this function.
	 *
	 * @param string $function
	 * @return mixed
	 */
	public function __call($function, $args)
	{
		//return call_user_func_array(array($this->_object, $function), $args);
		$debug = Debug::getInstance()->isDebugMode();

		// determite if it's 'get' or 'set'
		$action = substr($function, 0, 3);
		// replacing 'get' by empty string
		$name = substr_replace($function, '', 0, 3);

		// Change camel case to variable name (Ex: FullDesc -> full_desc)
		$filter = new Zend_Filter_Word_CamelCaseToUnderscore();
		$varName = strtolower($filter->filter($name));

		// get list of class variables
		$vars = array_keys(get_object_vars($this));
		$class = get_class($this);
		if (!in_array($varName, $vars)) {
			if (!$debug) {
				return null;
			} else {
				throw new Exception("Variable $varName is not defined in class $class");
			}
		}
		if ($action === 'get') {
			return $this->$varName;
		} elseif ($action === 'set') {
			$this->$varName = $args[0];
		} else {
			if ($debug) {
				throw new Exception("Wrong method name. There're only getters and setters aviable");
			}
		}

	}

	public function __get($name) {
		return $this->$name;
	}

	public function __set($name, $value){
		$this->$name = $value;
	}
}